<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Queue\Capsule\Manager;
use Session;
use DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Log;
use App\Http\Requests\CreateServeProjectRequest;
use App\Http\Requests\UpdateServeProjectRequest;
use App\ServeProject;
use App\StateConfig;
use Illuminate\Support\Facades\Auth;

class ServeProjectController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

	public function index(Request $request) {
		// 查看当前菜单可使用的功能按钮
		$uri = $request->path();
		$data['optionArr'] =  $this->checkMenuOption($uri);

		$serveObj = ServeProject::where('is_del',1)->get();
//		dd($serveObj);

		$data['serveProjects'] = $serveObj;

		return view('serveProject.list')->with($data);
	}

    public function create()
    {
	    $data['serveType'] = StateConfig::getAllState('serveType');// 获取服务的类型

        return view('serveProject.create')->with($data);
    }


    public function store(CreateServeProjectRequest $request){
        $serveProjectObj = new ServeProject();
	    $serveProjectObj->serveName =$request->serveName;
	    $serveProjectObj->serveType =$request->serveType;
	    $serveProjectObj->serveMoney =$request->serveMoney;
	    $serveProjectObj->serveDesc =$request->serveDesc;
	    $serveProjectObj->validity =$request->month;
	    $serveProjectObj->remark =$request->remark;

        if($serveProjectObj->save()){

	        $adminUser = Auth::user();
	        if($adminUser){
		        $userId = $adminUser->id;
		        $logs = $adminUser->name.'在'.date('Y-m-d H:i:s').' 新增了服务项目：【  '.$request->serveName.' 】';
		        adminOperationLog($request,$userId,$logs);
	        }

            return redirect('serveProject/list')->with('success','新增成功');
        }else{
            return redirect()->back()->withInput();
        }
    }


	public function show($id)
	{
		// 获取服务项目下的订单情况
		$serveProjectOrder = ServeProject::getServeProjectOrder($id);
//		dd($serveProjectOrder);

		$data['serveProjectOrder'] = $serveProjectOrder;

		return view('serveProject.show')->with($data);

	}


    public function edit($id)
    {
	    $serveProjectObj = ServeProject::find($id);
        $data['serveProjectInfo'] = $serveProjectObj;

	    $data['serveType'] = StateConfig::getAllState('serveType');// 获取服务的类型
//        dd($serveProjectObj);

        return view('serveProject.edit')->with($data);

    }


    public function update(UpdateServeProjectRequest $request, $id){

	    $serveProjectObj = ServeProject::find($id);
	    $serveProjectObj->serveName =$request->serveName;
	    $serveProjectObj->serveType =$request->serveType;
	    $serveProjectObj->serveMoney =$request->serveMoney;
	    $serveProjectObj->serveDesc =$request->serveDesc;
	    $serveProjectObj->validity =$request->month;
	    $serveProjectObj->remark =$request->remark;

        if(!$serveProjectObj->save()){
            return Redirect::back()->withInput()->withErrors(['fail' => trans('message.updateFail')]);
        }

	    $adminUser = Auth::user();
	    if($adminUser){
		    $userId = $adminUser->id;
		    $logs = $adminUser->name.'在'.date('Y-m-d H:i:s').'编辑了服务：【 '.$request->serveName.' 】';
		    adminOperationLog($request,$userId,$logs);
	    }

        return Redirect::to(url('serveProject/list'))->with('success', trans('message.updateSuccess'));

    }

    public function delete(Request $request){
		$eid = $request->id;
        if(!$eid){
            return '-1';
        }

        //  del
	    $affectId= ServeProject::where('id',$eid)->update([
		    'is_del'=>-1
	    ]);
//	    $affectId = ServeProject::where('id',$eid)->delete();
        if(!$affectId){
            return -1;
        }

	    $name = ServeProject::getNameById($eid);
	    $adminUser = Auth::user();
	    if($adminUser){
		    $userId = $adminUser->id;
		    $logs = $adminUser->name.'在'.date('Y-m-d H:i:s').'删除了服务：【 '.$name.' 】';
		    adminOperationLog($request,$userId,$logs);
	    }

        return 1;

    }


}





